<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>智慧消防云平台 - 注册</title>
    <script src="js/jquery-3.7.1.js"></script>
    <style>
        /* 全局重置与基础设置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Microsoft YaHei", sans-serif;
        }
        body {
            background: url('images/true.jpg') no-repeat center center/cover;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: #fff;
        }

        /* 注册容器：左右分栏布局 */
        .register-container {
            width: 900px;
            height: 580px;
            background: #fff;
            display: flex;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 0 20px rgba(0,0,0,0.3);
        }

        /* 左侧表单区域 */
        .register-left {
            flex: 1;
            padding: 40px 50px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        .register-left h2 {
            font-size: 28px;
            color: #00bfa5;
            margin-bottom: 30px;
        }

        /* 表单组通用样式 */
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
        }
        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        .form-group input:focus,
        .form-group select:focus {
            border-color: #00bfa5;
            outline: none;
        }

        /* 注册按钮 */
        .btn-register {
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, #00c853, #00bfa5);
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 10px;
            transition: opacity 0.3s;
        }
        .btn-register:hover {
            opacity: 0.9;
        }

        /* 登录链接 */
        .login-link {
            margin-top: 20px;
            font-size: 14px;
            color: #999;
            text-align: center;
        }
        .login-link a {
            color: #00bfa5;
            text-decoration: none;
            font-weight: 500;
        }
        .login-link a:hover {
            text-decoration: underline;
        }

        /* 右侧视觉区域（使用与登录页相同的图片） */
        .register-right {
            width: 45%;
            background: url('images/665bb057647420f.jpg') no-repeat center center/cover;
        }
    </style>
</head>
<body>
<div class="register-container">
    <!-- 左侧表单 -->
    <div class="register-left">
        <h2>用户注册</h2>
        <div id="error-message" class="error-message" style="color: red; margin-bottom: 15px; display: none;"></div>
        <form id="register-form">
            <div class="form-group">
                <label for="txtname">用户名</label>
                <input type="text" id="txtname" name="txtname" placeholder="请输入用户名" required>
            </div>
            <div class="form-group">
                <label for="txtpwd">密码</label>
                <input type="password" id="txtpwd" name="txtpwd" placeholder="请输入密码" required>
            </div>
            <!-- 角色选择 -->
            <div class="form-group">
                <label for="role">用户角色</label>
                <select id="role" name="role" required>
                    <option value="">请选择角色</option>
                    <option value="管理人员">管理人员</option>
                    <option value="检查人员">检查人员</option>
                </select>
            </div>
            <button type="submit" class="btn-register">注册</button>
            <div class="login-link">
                已有账号？<a href="index.jsp">立即登录</a>
            </div>
        </form>
    </div>
    <!-- 右侧背景图/视觉区 -->
    <div class="register-right"></div>
</div>

<script>
    $(document).ready(function() {
        $("#register-form").submit(function(e) {
            e.preventDefault();
            // 校验角色是否选择
            let roleVal = $("#role").val();
            if (roleVal === "") {
                $("#error-message").text("请选择用户角色").show();
                return;
            }
            var data = {
                "txtname": $("#txtname").val(),
                "txtpwd": $("#txtpwd").val(),
                "role": roleVal
            };
            $.post("RegServlet", data, function(response) {
                if (response === "注册成功！") {
                    alert("注册成功，请登录");
                    window.location.href = "index.jsp";
                } else {
                    $("#error-message").text(response).show();
                }
            }).fail(function(xhr, status, error) {
                console.error("注册请求失败：", status, error);
                console.error("响应内容：", xhr.responseText);
                $("#error-message").text("网络错误或服务器异常，请重试").show();
            });
        });
    });
</script>
</body>
</html>